package com.financial.service.platbalancepay.service.impl;

import com.aliyun.openservices.shade.com.alibaba.fastjson.JSON;
import com.aliyun.openservices.shade.com.alibaba.fastjson.serializer.SerializerFeature;
import com.financial.service.platbalancepay.config.UrlConstants;
import com.financial.service.platbalancepay.connector.PlatformPayFegin;
import com.financial.service.platbalancepay.connector.YspayFegin;
import com.financial.service.platbalancepay.constants.FinancialPayreceiptPlatbalanceEnumConstants;
import com.financial.service.platbalancepay.constants.FinancialPlatfromConfigEnumConstants;
import com.financial.service.platbalancepay.constants.FinancialReceiptBalancepayEnumConstants;
import com.financial.service.platbalancepay.dao.FinancialReqparamPlatbalancepayDao;
import com.financial.service.platbalancepay.dao.GybalanceWechatagencyReceiptDao;
import com.financial.service.platbalancepay.dao.GybalanceWechatpayReceiptDao;
import com.financial.service.platbalancepay.domain.FinancialReceiptBalancepayDO;
import com.financial.service.platbalancepay.domain.FinancialReqparamPlatbalancepayDO;
import com.financial.service.platbalancepay.domain.GybalanceWechatagencyReceiptDO;
import com.financial.service.platbalancepay.domain.GybalanceWechatpayReceiptDO;
import com.financial.service.platbalancepay.service.BalancePayService;
import com.financial.service.platbalancepay.utils.DateUtils;
import com.financial.service.platbalancepay.utils.apiresult.ApiResult;
import com.financial.service.platbalancepay.utils.balanceutils.AccessTokenUtil;
import com.financial.service.platbalancepay.utils.balanceutils.HttpClientUtils;
import com.financial.service.platbalancepay.utils.balanceutils.JsonUtils;
import com.financial.service.platbalancepay.utils.balanceutils.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class BalancePayServiceImpl implements BalancePayService {
    @Autowired
    private HttpClientUtils httpClientUtils;
    @Autowired
    private AccessTokenUtil accessTokenUtil;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private YspayFegin yspayFegin;
    @Autowired
    private PlatformPayFegin platformPayFegin;
    @Autowired
    private FinancialReqparamPlatbalancepayDao financialReqparamPlatbalancepayDao;
    @Autowired
    private BalancePayService financialReceiptBalancepayService;
    @Autowired
    private GybalanceWechatpayReceiptDao gybalanceWechatpayReceiptDao;
    @Autowired
    private GybalanceWechatagencyReceiptDao gybalanceWechatagencyReceiptDao;
    public static final Logger logger = LoggerFactory.getLogger(BalancePayServiceImpl.class);


    @Override
    public Map<String, Object> ysWechatBalancePay(Map<String, Object> paramMap) {
        logger.info("BalancePayServiceImpl-----------平台余额支付接口入参：{}", JSON.toJSONString(paramMap));

        Map<String, Object> resultMap = null;
        //参数校验
        Boolean bo = verifyParam(paramMap);
        if (!bo) {
            return ApiResult.paramIsNull();
        }

        //保存平台余额预支付信息
        Map<String, Object> saveMap = saveReqparamPay(paramMap);
        String customerUuid = paramMap.get("customerUuid").toString();
        //支付单号
        String outTradeNo = paramMap.get("out_trade_no").toString();//总金额
        String totalAmount = paramMap.get("total_amount").toString();
        //调用银盛微信支付之前MQ发送够样冻结参数（老够样）
        Map<String, Object> map = new HashMap<>();
        map.put("billNo", outTradeNo);
        map.put("frozenBalance", totalAmount);
        map.put("customerUuid", customerUuid);
        String msg = JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect);

      /*  try {
            Message message = new Message(BalancePayMQConfig.BALANCEPAY_TOPIC, BalancePayMQConfig.BALANCE_FREEZE, msg.getBytes());
            logger.info("MQ发送够样冻结信息：{}", new String(message.getBody()));
            producer.send(message);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("MQ发送够样冻结信息失败！：用户id：{},支付单号：{}", customerUuid, outTradeNo);
        }*/
        //feign接口调用银盛微信小程序支付
        logger.info("feign接口调用银盛微信小程序支付参数为：{}", JSON.toJSONString(paramMap));
        try {
            Object temp = "";
            Map<String, Object> ysMap = yspayFegin.miniAppPay(paramMap);
            String ysoutTradeNo = ysMap.get("out_trade_no").toString();
            String trade_status = ysMap.get("trade_status").toString();
            String total_amount = ysMap.get("total_amount").toString();
            temp = ysMap.get("trade_no");
            String trade_no = temp != null ? temp.toString() : "";
            //---------------------------------------
            //余额支付同步回执保存
            FinancialReceiptBalancepayDO financialReceiptBalancepayDO = new FinancialReceiptBalancepayDO();
            financialReceiptBalancepayDO.setResponseType(FinancialPayreceiptPlatbalanceEnumConstants.ENUM_RESPONSETYPE_SYNC);
            financialReceiptBalancepayDO.setCustomeruuid(saveMap.get("customerUuid").toString());
            financialReceiptBalancepayDO.setOutTradeNo(map.get("out_trade_no").toString());
            financialReceiptBalancepayDO.setPlatfromAppid(saveMap.get("platfromId").toString());
            financialReceiptBalancepayDO.setPayChannelsid(saveMap.get("channelsId").toString());
            financialReceiptBalancepayDO.setNotifyTime(ysMap.get("notify_time").toString());
            financialReceiptBalancepayDO.setCashAmount(new BigDecimal(paramMap.get("cash_amount").toString()));
            financialReceiptBalancepayDO.setPlatfromAppid(saveMap.get("mer_amount").toString());
            financialReceiptBalancepayDO.setTotalAmount(new BigDecimal(saveMap.get("total_amount").toString()));
            financialReceiptBalancepayDO.setTradeNo(temp != null ? temp.toString() : "");
            financialReceiptBalancepayDO.setTradeStatus(ysMap.get("trade_status").toString());
            financialReceiptBalancepayDO.setTradeNo(ysMap.get("account_date").toString());
            financialReceiptBalancepayDO.setGmtCreate(new Date());
            financialReceiptBalancepayDO.setGmtModified(new Date());
            financialReceiptBalancepayDO.setIsDelete(FinancialPayreceiptPlatbalanceEnumConstants.ENUM_ISDELETE_UNDEL);
            financialReceiptBalancepayService.save(financialReceiptBalancepayDO);
            //将银盛同步回执返回平台
            resultMap.put("out_trade_no", ysoutTradeNo);
            resultMap.put("trade_no", trade_no);
            resultMap.put("total_amount", total_amount);
            resultMap.put("trade_status", trade_status);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("feign接口调用银盛微信小程序支付失败！", e.getMessage());
        }
        return resultMap;
    }


    private Map<String, Object> saveReqparamPay(Map<String, Object> paramMap) {
        int platfromId = 0;
        int channelsId = 0;
        //用户id
        String customerUuid = paramMap.get("customerUuid").toString();
        //支付通道
        String payType = paramMap.get("payType").toString();
        //平台应用名称
        String platfromAppname = paramMap.get("platfrom_appname").toString();
        //支付单号
        String outTradeNo = paramMap.get("out_trade_no").toString();
        //现金支付金额
        String cash_amount = paramMap.get("cash_amount").toString();
        //营销账户支付金额
        String mer_amount = paramMap.get("mer_amount").toString();
        //总金额
        String totalAmount = paramMap.get("total_amount").toString();
        //根据平台应用名称获取id
        try {
            platfromId = platformPayFegin.getPlatfromId(platfromAppname);
            //根据支付通道获取id
            channelsId = platformPayFegin.getChannelsId(payType);
            paramMap.put("platfromId", String.valueOf(platfromId));
            paramMap.put("channelsId", String.valueOf(channelsId));
            //保存平台余额预支付信息

            FinancialReqparamPlatbalancepayDO frqparam = new FinancialReqparamPlatbalancepayDO();
            frqparam.setCustomeruuid(customerUuid);
            frqparam.setGmtCreate(new Date());
            frqparam.setGmtModified(new Date());
            frqparam.setIsDelete(FinancialPayreceiptPlatbalanceEnumConstants.ENUM_ISDELETE_UNDEL);
            frqparam.setOutTradeNo(outTradeNo);
            frqparam.setPlatfromAppid(platfromId);
            frqparam.setPayChannelsid(channelsId);
            frqparam.setTotalAmount(new BigDecimal(totalAmount));
            frqparam.setCashAmount(new BigDecimal(cash_amount));
            frqparam.setMerAmount(new BigDecimal(mer_amount));
            financialReqparamPlatbalancepayDao.save(frqparam);
            logger.info("平台余额支付调用银盛微信小程序入参" + frqparam.toString());
       /* } catch (NullPointerException e) {
            e.printStackTrace();
            logger.error("获取支付通道id{},平台应用id{}失败！", channelsId, platfromId);*/
        } catch (Exception e1) {
            e1.printStackTrace();
            logger.error("保存平台余额预支付信息失败！" + "用户id为" + customerUuid + "订单号为" + outTradeNo + e1);
        }
        return paramMap;
    }

    private Boolean verifyParam(Map<String, Object> paramMap) {
        //支付单号
        String outTradeNo = paramMap.get("out_trade_no").toString();
        //总金额
        String totalAmount = paramMap.get("total_amount").toString();
        //用户id
        String customerUuid = paramMap.get("customerUuid").toString();
        //支付通道
        String payType = paramMap.get("payType").toString();
        //平台应用名称
        String platfromAppname = paramMap.get("platfrom_appname").toString();
        //现金支付金额
        String cash_amount = paramMap.get("cash_amount").toString();
        //营销账户支付金额
        String mer_amount = paramMap.get("mer_amount").toString();
        //交易描述
        String subject = paramMap.get("subject").toString();
        // 微信用户id
        String sub_openid = paramMap.get("sub_openid").toString();

        if (StringUtils.isEmpty(outTradeNo)) {
            logger.info("BalancePayServiceImpl --->verifyParam方法参数校验失败：订单号为{}", outTradeNo);
            return false;
        }
        if (StringUtils.isEmpty(totalAmount)) {
            logger.info("BalancePayServiceImpl --->verifyParam方法参数校验失败：总金额为{}", totalAmount);
            return false;
        }
        if (StringUtils.isEmpty(customerUuid)) {
            logger.info("BalancePayServiceImpl --->verifyParam方法参数校验失败：用户id为{}", customerUuid);
            return false;
        }
        if (StringUtils.isEmpty(payType)) {
            logger.info("BalancePayServiceImpl --->verifyParam方法参数校验失败：支付通道类型为{}", payType);
            return false;
        }
        if (StringUtils.isEmpty(platfromAppname)) {
            logger.info("BalancePayServiceImpl --->verifyParam方法参数校验失败：平台应用名称为{}", platfromAppname);
            return false;
        }
        if (StringUtils.isEmpty(cash_amount)) {
            logger.info("BalancePayServiceImpl --->verifyParam方法参数校验失败：现金支付金额为{}", cash_amount);
            return false;
        }

        if (StringUtils.isEmpty(mer_amount)) {
            logger.info("BalancePayServiceImpl --->verifyParam方法参数校验失败：优惠金额为{}", mer_amount);
            return false;
        }
        if (StringUtils.isEmpty(subject)) {
            logger.info("BalancePayServiceImpl --->verifyParam方法参数校验失败：交易描述为{}", subject);
            return false;
        }
        if (StringUtils.isEmpty(sub_openid)) {
            logger.info("BalancePayServiceImpl --->verifyParam方法参数校验失败：微信用户id为{}", sub_openid);
            return false;
        }
        return true;
    }


    public String balancePayCallBack(Map<String, String> paramMap, HttpServletResponse response) {
     /*   String outTradeNo = "";
        String totalAmount = "";
        //
        outTradeNo = paramMap.get("out_trade_no");
        //根据支付单号获取平台支付应用id,支付通道id
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("out_trade_no", outTradeNo);
        requestMap.put("is_delete", FinancialPayreceiptPlatbalanceEnumConstants.ENUM_ISDELETE_UNDEL);
        List<FinancialReqparamPlatbalancepayDO> queryList = financialReqparamPlatbalancepayDao.list(requestMap);
        FinancialReqparamPlatbalancepayDO paramDo = queryList != null && queryList.size() > 0 ? queryList.get(0) : null;
        Integer payChannelsid = paramDo.getPayChannelsid();
        Integer platfromAppid = paramDo.getPlatfromAppid();
        //根据平台支付应用id，支付通道id获取对应配置信息
        paramMap.put("payChannelsid", String.valueOf(payChannelsid));
        paramMap.put("platfromAppid", String.valueOf(platfromAppid));
        //发起银盛微信小程序支付
        logger.info("feign接口调用银盛微信异步接口参数为：{}", JSON.toJSONString(paramMap));
        try {
            yspayFegin.miniAppCallBack(paramMap, response);

            Map<String, Object> map = new HashMap<>();

            String tradeStatus = paramMap.get("trade_status");
            outTradeNo = paramMap.get("out_trade_no");
            totalAmount = paramMap.get("total_amount");
            //根据outTradeNo获取customerUuid
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("out_trade_no", outTradeNo);
            resultMap.put("is_delete", FinancialPayreceiptPlatbalanceEnumConstants.ENUM_ISDELETE_UNDEL);
            List<FinancialReqparamPlatbalancepayDO> list = financialReqparamPlatbalancepayService.list(resultMap);
            FinancialReqparamPlatbalancepayDO financialReqparamPlatbalancepayDO = list != null && list.size() > 0 ? list.get(0) : null;
            String customeruuid = financialReqparamPlatbalancepayDO.getCustomeruuid();
            //判断异步交易状态 这种情况请求银盛成功
            if (!StringUtils.isEmpty(tradeStatus) && tradeStatus.equals("TRADE_SUCCESS")) {
                map.put("outTradeNo", outTradeNo);
                map.put("totalAmount", new BigDecimal(totalAmount));
                map.put("flag", "1");
                String unfreezemsg = JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect);
                Message unfreezeMessage = new Message(BalancePayMQConfig.BALANCEPAY_TOPIC, BalancePayMQConfig.BALANCE_UNFREEZE, unfreezemsg.getBytes());
                logger.info("微信余额支付交易成功通知够样平台核减操作: {}", new String(unfreezeMessage.getBody()));
                producer.send(unfreezeMessage);
            } else if (!tradeStatus.equals("TRADE_SUCCESS") && !StringUtils.isEmpty(tradeStatus)) {
                map.put("outTradeNo", outTradeNo);
                map.put("totalAmount", new BigDecimal(totalAmount));
                map.put("flag", "0");
                String freezemsg = JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect);
                //todo
                Message freezeMessage = new Message(BalancePayMQConfig.BALANCEPAY_TOPIC, BalancePayMQConfig.BALANCE_UNFREEZE, freezemsg.getBytes());
                producer.send(freezeMessage);
            } else {
                //查询银盛微信小程序支付
                Map<String, String> querymap = new HashMap<>();
                querymap.put("out_trade_no", outTradeNo);
                querymap.put("is_delete", FinancialPayreceiptPlatbalanceEnumConstants.ENUM_ISDELETE_UNDEL);
                logger.info("balancePayCallBack------银盛微信余额查询接口参数：{}", JSON.toJSONString(querymap));
                Map<String, Object> reMap = yspayFegin.ysPayQuery(querymap);
                if (reMap.get("trade_status").equals("TRADE_SUCCESS")) {
                    map.put("outTradeNo", outTradeNo);
                    map.put("totalAmount", new BigDecimal(totalAmount));
                    map.put("flag", "1");
                    String unfreezemsg = JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect);
                    Message message = new Message(BalancePayMQConfig.BALANCEPAY_TOPIC, BalancePayMQConfig.BALANCE_UNFREEZE, JSONUtils.objectToJson(unfreezemsg).getBytes());
                    // 设置延迟时间 50s
                    message.setStartDeliverTime(System.currentTimeMillis() + 50000);
                    logger.error("查询微信小程序支付" + new String(message.getBody()));
                    producer.send(message);
                } else {
                    map.put("outTradeNo", outTradeNo);
                    map.put("totalAmount", new BigDecimal(totalAmount));
                    map.put("flag", "0");
                    String freezemsg = JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect);
                    Message freezeMessage = new Message(BalancePayMQConfig.BALANCEPAY_TOPIC, BalancePayMQConfig.BALANCE_FREEZE, freezemsg.getBytes());
                    producer.send(freezeMessage);
                }


            }
        } catch (Exception e) {
            Map<String, Object> resultmap = new HashMap<>();
            Map<String, String> querymap = new HashMap<>();
            querymap.put("out_trade_no", outTradeNo);
            querymap.put("is_delete", FinancialPayreceiptPlatbalanceEnumConstants.ENUM_ISDELETE_UNDEL);
            Map<String, Object> resMap = yspayFegin.ysPayQuery(querymap);
            if (resMap.get("trade_status").equals("TRADE_SUCCESS")) {
                resultmap.put("outTradeNo", outTradeNo);
                resultmap.put("totalAmount", totalAmount);
                resultmap.put("flag", "1");
                String unfreezemsg = JSON.toJSONString(resultmap, SerializerFeature.DisableCircularReferenceDetect);
                Message message = new Message(BalancePayMQConfig.BALANCEPAY_TOPIC, BalancePayMQConfig.BALANCE_UNFREEZE, JSONUtils.objectToJson(unfreezemsg).getBytes());
                // 设置延迟时间 50s
                message.setStartDeliverTime(System.currentTimeMillis() + 50000);
                logger.error("查询微信小程序支付" + new String(message.getBody()));
                producer.send(message);
            } else {
                resultmap.put("outTradeNo", outTradeNo);
                resultmap.put("totalAmount", totalAmount);
                resultmap.put("flag", "0");
                String freezemsg = JSON.toJSONString(resultmap, SerializerFeature.DisableCircularReferenceDetect);
                Message freezeMessage = new Message(BalancePayMQConfig.BALANCEPAY_TOPIC, BalancePayMQConfig.BALANCE_FREEZE, freezemsg.getBytes());
                producer.send(freezeMessage);
            }
            e.printStackTrace();
        }
*/
        return null;

    }


    @Override
    public Map<String, Object> wechatBalancePay(Map<String, Object> reqMap) {
        logger.info("wechatBalancePay-------优店余额支付请求金融中心余额入参"+ JsonUtils.objectToJson(reqMap));
        Map<String, Object> chageMap = null;
        String result = null;
        //订单号
        String balanceOrderNum = reqMap.get("orderNum").toString();
        //会员id
        String customerUuid = reqMap.get("customerUuid").toString();
        //余额支付总金额
        String payMoney = reqMap.get("payMoney").toString();
        //交易描述
        String subject = reqMap.get("subject").toString();

        String  platfromMarking = reqMap.get("platfromMarking").toString();
        if(StringUtils.isEmpty(balanceOrderNum)
                ||StringUtils.isEmpty(customerUuid)||StringUtils.isEmpty(payMoney)){
            return ApiResult.paramIsNull();
        }

        //根据平台标识获取平台应用id
        int platfromId = platformPayFegin.getPlatfromId(platfromMarking);

        //缓存中获取token
        String accessToken = null;
        Object object = redisUtils.getValue("custumerAccessToken");
        if (null == object) {
            accessToken = accessTokenUtil.getaccessToken();
            logger.info("优店余额支付获取金融中心token"+accessToken);
        } else {
            accessToken = (String) object;
        }
        //拼接请求参数对接够样平台
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("customerUuid",customerUuid);
        requestMap.put("orderNum",balanceOrderNum);
        requestMap.put("payMoney",payMoney);
        requestMap.put("registBusNum",platfromMarking);
        requestMap.put("accessToken",accessToken);
        //余额平台回调地址
        logger.info("拼接请求参数对接够样平台:{}",JsonUtils.objectToJson(requestMap));
        Map<String, Object> prePayInfoMap = null;
        try {
            result = httpClientUtils.doPost(UrlConstants.BALANCE_MAKEPAY_URL, requestMap);
            chageMap = JsonUtils.jsonToMap(result);
            if(!"0".equals(chageMap.get("return_code"))){
                ApiResult.failure("预支付单获取失败");
            }
            logger.info("++++customerUuid:{},余额预支付信息结果:{}", requestMap.get("customerUuid"),JsonUtils.jsonToMap(result));
            //保存够样余额预支付同步回执
            String return_code = chageMap.get("return_code").toString();
            String message = chageMap.get("message").toString();
            String prePayInfoDO = chageMap.get("prePayInfoDO").toString();

            prePayInfoMap = JsonUtils.jsonToMap(prePayInfoDO);
            String customerUuidsync = prePayInfoMap.get("customerUuid").toString();
            String opeTime = prePayInfoMap.get("opeTime").toString();
            String payMoneysync = prePayInfoMap.get("payMoney").toString();
            String prePayNo = prePayInfoMap.get("prePayNo").toString();
            String state = prePayInfoMap.get("state").toString();
            String orderNumsync = prePayInfoMap.get("orderNum").toString();

            //保存回执信息
            GybalanceWechatpayReceiptDO balanceReceiptDO = new GybalanceWechatpayReceiptDO();
            balanceReceiptDO.setCode(return_code);
            balanceReceiptDO.setCustomeruuid(customerUuidsync);
            balanceReceiptDO.setGmtCreate(new Date());
            balanceReceiptDO.setGmtModified(new Date());
            balanceReceiptDO.setIsDelete("1");
            balanceReceiptDO.setMsg(message);
            balanceReceiptDO.setOpetime(opeTime);
            balanceReceiptDO.setOrderno(orderNumsync);
            balanceReceiptDO.setPlatfromappid(platfromId+"");
            balanceReceiptDO.setPaymoney(new BigDecimal(payMoneysync));
            balanceReceiptDO.setPrepayno(prePayNo);
            balanceReceiptDO.setState(state);
            balanceReceiptDO.setResponseType("1");
            balanceReceiptDO.setSubject(subject);
            gybalanceWechatpayReceiptDao.save(balanceReceiptDO);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("++++customerUuid:{},余额预支付信息失败:{},", customerUuid, e.getMessage());
            return  ApiResult.failure("预支付单处理失败");
        }
        return ApiResult.success(JsonUtils.objectToJson(prePayInfoMap));
    }

    @Override
    public Map<String, Object> wechatBalanceasynPay(Map<String, Object> reqMap){
        logger.info("金融支付中心接收够样余额中心信息"+JsonUtils.objectToJson(reqMap));
        String orderNum ="";
        try {
        String balanceCode = reqMap.get("balanceCode").toString();
        String message = reqMap.get("message").toString();
        String prePayNo = reqMap.get("prePayNo").toString();
        String payMoney = reqMap.get("payMoney").toString();
        String customerUuid = reqMap.get("customerUuid").toString();
        orderNum = reqMap.get("orderNum").toString();
        String registBusNum = reqMap.get("registBusNum").toString();
        int platfromId = platformPayFegin.getPlatfromId(registBusNum);
        //todo 余额预支付异步信息通知优点平台回执记录
        GybalanceWechatpayReceiptDO gybalanceWechatpayReceiptDO = new GybalanceWechatpayReceiptDO();
        gybalanceWechatpayReceiptDO.setResponseType("2");
        gybalanceWechatpayReceiptDO.setPrepayno(prePayNo);
        gybalanceWechatpayReceiptDO.setPaymoney(new BigDecimal(payMoney).setScale(2, RoundingMode.HALF_UP));
        gybalanceWechatpayReceiptDO.setCustomeruuid(customerUuid);
        gybalanceWechatpayReceiptDO.setCode(balanceCode);
        gybalanceWechatpayReceiptDO.setSubject("交易描述");
        gybalanceWechatpayReceiptDO.setGmtCreate(new Date());
        gybalanceWechatpayReceiptDO.setGmtModified(new Date());
        gybalanceWechatpayReceiptDO.setIsDelete("1");
        gybalanceWechatpayReceiptDO.setMsg(message);
        gybalanceWechatpayReceiptDO.setOpetime(DateUtils.getNowTimeStr());
        gybalanceWechatpayReceiptDO.setOrderno(orderNum);
        gybalanceWechatpayReceiptDO.setPlatfromappid(String.valueOf(platfromId));
        if (balanceCode.equals("0")){
            gybalanceWechatpayReceiptDO.setState("支付完成");
        }
            gybalanceWechatpayReceiptDao.save(gybalanceWechatpayReceiptDO);
            String result = httpClientUtils.doPost(UrlConstants.PLATFROM_BALANCE_ASYN_URL, reqMap);
            logger.info("余额支付异步结果集"+result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("orderNum:{},余额支付平台回调够样优店失败:{}",orderNum,e.getMessage());
        }
        return null;
    }

    @Override
    public String queryBalancepay(Map<String,Object> paraMap) {
        logger.info("接收优点余额支付查询参数"+JsonUtils.objectToJson(paraMap));
        String resultStr ="";
        //缓存中获取token
        String accessToken = null;
        Object object = redisUtils.getValue("custumerAccessToken");
        if (null == object) {
            accessToken = accessTokenUtil.getaccessToken();
        } else {
            accessToken = (String) object;
        }
        paraMap.put("accessToken",accessToken);
        try {
            resultStr = httpClientUtils.doPost(UrlConstants.BALANCE_PAY_QUERY_URL, paraMap);
            logger.info("调用够样余额主动查询返回信息"+resultStr);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("订单号:{},查询预支付信息失败!"+JsonUtils.objectToJson(paraMap));
        }
        return resultStr;
    }

    @Override
    public Map<String, Object> disposeBalanceasynPay(Map<String, Object> result) {
        logger.info("金融中心余额支付接受银盛代付异步回执消息"+JsonUtils.objectToJson(result));
        String platfromId = result.get("platfromId").toString();
        String outTradeNo = result.get("out_trade_no").toString();
        String tradeStatus = result.get("trade_status").toString();
        String totalAmount = result.get("total_amount").toString();
        GybalanceWechatagencyReceiptDO agencyReceiptDO = new GybalanceWechatagencyReceiptDO();
        agencyReceiptDO.setIsDelete(FinancialPlatfromConfigEnumConstants.ENUM_ISDELETE_UNDEL);
        agencyReceiptDO.setCode("10000");
        agencyReceiptDO.setMsg("交易成功");
        agencyReceiptDO.setTotalAmount(new BigDecimal(totalAmount));
        agencyReceiptDO.setTradeStatus(tradeStatus);
        agencyReceiptDO.setResponseType(FinancialReceiptBalancepayEnumConstants.ENUM_RESPONSETYPE_ASYNC);
        agencyReceiptDO.setGmtModified(new Date());
        agencyReceiptDO.setGmtCreate(new Date());
        agencyReceiptDO.setPlatfromappid(platfromId);
        agencyReceiptDO.setSubject("异步回执");
        agencyReceiptDO.setOutTradeNo(outTradeNo);
        gybalanceWechatagencyReceiptDao.save(agencyReceiptDO);
        logger.info("金融中心余额支付接受银盛代付异步结果集"+result);
        return result;
    }

    @Override
    public FinancialReceiptBalancepayDO get(Integer id) {
        return null;
    }

    @Override
    public List<FinancialReceiptBalancepayDO> list(Map<String, Object> map) {
        return null;
    }

    @Override
    public int count(Map<String, Object> map) {
        return 0;
    }


    @Override
    public int save(FinancialReceiptBalancepayDO financialReceiptBalancepay) {
        return 0;
    }

    @Override
    public int update(FinancialReceiptBalancepayDO financialReceiptBalancepay) {
        return 0;
    }

    @Override
    public int remove(Integer id) {
        return 0;
    }

    @Override
    public int batchRemove(Integer[] ids) {
        return 0;
    }


}
